<!-- subpkg_archive/list/index.vue -->
<script lang="ts" setup>
  import { patientListApi, removePatientApi } from '@/apis/patient'
  import { useConsultStore } from '@/stores/consult'
  import type { PatientItem } from '@/types/patient'
  import { objectToParams } from '@/utils/objectToParams'
  import { onPageShow } from '@dcloudio/uni-app'
  import { onMounted, ref } from 'vue'

  const consultStore = useConsultStore()
  const swipeOptions = ref([
    {
      text: '删除',
      style: {
        backgroundColor: '#dd524d',
        color: '',
        fontSize: '',
        height: '100%',
      },
    },
  ])

  const patientList = ref<PatientItem[]>([])
  const getPatientList = async () => {
    const res = await patientListApi()
    patientList.value = res
    if (!consultStore.patientID) {
      if (patientList.value.find((item) => item.defaultFlag === 1)) {
        consultStore.patientID = patientList.value.find((item) => item.defaultFlag === 1)!.id
      } else if (patientList.value.length > 0) {
        consultStore.patientID = patientList.value[0].id
      } else {
        consultStore.patientID = ''
      }
    }
  }
  onPageShow(() => {
    getPatientList()
  })
  // 侧滑删除时保存 id
  const activeId = ref('')
  const onDeleteItem = async (id: string) => {
    activeId.value = id
    const res = await uni.showModal({
      title: '提示',
      content: '确定要删除该患者吗？',
    })
    // 清除 id
    activeId.value = ''
    if (consultStore.patientID === id) {
      consultStore.patientID = ''
    }
    if (res.confirm) {
      await removePatientApi(id)
      await getPatientList()
      uni.showToast({
        title: '删除成功',
        icon: 'none',
      })
    }
  }

  // 兼容 选择患者 界面
  const props = defineProps<{
    illnessInfo?: Record<string, any>
    type?: 'consult'
  }>()

  // console.log(props.pictures)

  uni.setNavigationBarTitle({
    title: props.type === 'consult' ? '选择患者' : '患者档案',
  })

  const onNext = () => {
    uni.navigateTo({
      url: `/subpkg_consult/payment/payment?${objectToParams({
        patientID: consultStore.patientID,
      })}`,
    })
  }
</script>

<template>
  <scroll-page>
    <view class="archive-page">
      <view v-if="props.type === 'consult'">
        <view>请选择患者信息</view>
        <view class="archive-tips">以便医生给出更准确的治疗，信息仅医生可见</view>
      </view>
      <view v-if="props.type !== 'consult'" class="archive-tips">最多可添加6人</view>

      <uni-swipe-action>
        <uni-swipe-action-item
          v-for="item in patientList"
          :key="item.id"
          :right-options="swipeOptions"
          @click="onDeleteItem(item.id)"
          :show="activeId === item.id ? 'right' : 'none'"
        >
          <view
            :class="`archive-card ${
              consultStore.patientID === item.id && props.type === 'consult' ? 'active' : ''
            }`"
            @click="consultStore.patientID = item.id"
          >
            <view class="archive-info">
              <text class="name">{{ item.name }}</text>
              <text class="id-card">{{
                item.idCard.replace(/^(.{6}).+(.{4})$/, '$1********$2')
              }}</text>
              <text class="default" v-if="item.defaultFlag === 1">默认</text>
            </view>
            <view class="archive-info">
              <text class="gender">{{ item.genderValue }}</text>
              <text class="age">{{ item.age }}岁</text>
            </view>
            <navigator
              hover-class="none"
              class="edit-link"
              :url="`/subpkg_archive/form/form?id=${item.id}`"
            >
              <uni-icons type="icon-edit" size="20" color="#16C2A3" custom-prefix="iconfont" />
            </navigator>
          </view>
        </uni-swipe-action-item>
      </uni-swipe-action>

      <!-- 添加按钮 -->
      <view v-if="patientList.length < 6" class="archive-card">
        <navigator class="add-link" hover-class="none" url="/subpkg_archive/form/form">
          <uni-icons color="#16C2A3" size="24" type="plusempty" />
          <text class="label">添加患者</text>
        </navigator>
      </view>
      <view v-if="props.type == 'consult'" class="archive-tips">最多可添加6人</view>

      <button
        v-if="props.type === 'consult'"
        class="uni-button"
        @click="onNext"
        :disabled="consultStore.patientID === ''"
      >
        下一步
      </button>
    </view>
  </scroll-page>
</template>

<style lang="scss">
  @import './list.scss';

  .uni-button {
    // 定位到底部显示, 宽度占满
    position: fixed;
    bottom: 50rpx;
    left: 24rpx;
    right: 24rpx;
    width: calc(100% - 48rpx);
  }
</style>
